package com.snmp.test;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.util.Vector;

/**
 * 本类用于向管理进程发送Trap信息(本地测试发送Trap)
 * linux发送trap命令：
 *  snmptrap -v 2c -c public 192.168.77.128 "aaa" 1.3.6.1.4.1.2345 SNMPv2-MIB::sysLocation.0 s "just here"
 *
 */
public class SnmpUtilSendTrap {

    private Snmp snmp = null;

    private Address targetAddress = null;

    public void initComm() throws IOException {

        // 设置管理进程的IP和端口
//        targetAddress = GenericAddress.parse("udp:127.0.0.1/162");
//        targetAddress = GenericAddress.parse("udp:192.168.77.1/162");
        targetAddress = GenericAddress.parse("udp:192.168.77.128/162");
        TransportMapping transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();

    }

    /**
     * 向管理进程发送Trap报文
     *
     * @throws IOException
     */
    public void sendPDU() throws IOException {

        // 设置 target
        CommunityTarget target = new CommunityTarget();
        target.setAddress(targetAddress);

        // 通信不成功时的重试次数
        target.setRetries(2);
        // 超时时间
        target.setTimeout(1500);
        // snmp版本
        target.setVersion(SnmpConstants.version2c);

        // 创建 PDU
        PDU pdu = new PDU();
        //ALARM_SN
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.1"),
                new OctetString("123")));
        //TYPE
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.2"),
                new OctetString("1")));
        //AlarmOccurTime 告警发生时间
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.3"),
                new OctetString("2019-06-11 17:37:28")));
        //OBJECT_NAME
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.4"),
                new OctetString("Test")));
        //ALARM_NAME
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.5"),
                new OctetString("1232132")));
        //ne_type  设备类型标识(填写网元类型)
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.6"),
                new OctetString("无线专网")));
        //OLD_ADDITIONAL
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.7"),
                new OctetString("Test")));
        //FIRST_REPORT_TIME
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.8"),
                new OctetString("2018-12-25 08:00:00.000")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.9"),
                new OctetString("323133")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.10"),
                new OctetString("323133")));

        //alarm_level 告警级别:	0:事件告警没有级别概念,字段为0; 1:紧急; 2:重要; 3:次要; 4:提示; 6:恢复
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.11"),
                new OctetString("3")));
        //TYPE
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.12"),
                new OctetString("1")));
        //SEVERITY
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.13"),
                new OctetString("3")));
        //OBJECT_NAME
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.14"),
                new OctetString("Test")));
        //ALARM_NAME
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.15"),
                new OctetString("Test")));
        //OLD_ALARM_SOURCE_OBJ_IP
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.16"),
                new OctetString("172.25.241.215")));
        //OLD_ADDITIONAL
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.27"),
                new OctetString("Test")));
        //FIRST_REPORT_TIME
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.28"),
                new OctetString("2018-12-25 08:00:00.000")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.29"),
                new OctetString("8899")));

        //alarm_specificproblems 告警详细原因
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.30"),
                new OctetString("设备故障")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.46"),
                new OctetString("77")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.47"),
                new OctetString("66")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.48"),
                new OctetString("56")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.49"),
                new OctetString("444")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.50"),
                new OctetString("333")));

        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.51"),
                new OctetString("234")));
        //ne_ip_addr 触发告警的网元IP
        pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.42400.2.15.2.4.3.3.52"),
                new OctetString("10.8.170.129")));

        pdu.setType(PDU.TRAP);

        // 向Agent发送PDU，并接收Response
        ResponseEvent respEvnt = snmp.send(pdu, target);

        // 解析Response
        if (respEvnt != null && respEvnt.getResponse() != null) {
            Vector<? extends VariableBinding> recVBs = (Vector<? extends VariableBinding>) respEvnt.getResponse()
                    .getVariableBindings();
            for (int i = 0; i < recVBs.size(); i++) {
                VariableBinding recVB = recVBs.elementAt(i);
                System.out.println(recVB.getOid() + " : " + recVB.getVariable());
            }
        }
    }

    public static void main(String[] args) {
        try {
            SnmpUtilSendTrap util = new SnmpUtilSendTrap();
            util.initComm();
            util.sendPDU();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
